BuildingSelected .equ appbackupscreen + 30

#include "Macros.asm"

BasicBuildings .equ 1
ResearchBuildings .equ 2

asm_flag1_2 .equ 2
#define Object_Ready_To_Add asm_flag1_2



AddObject:


 BIT Select_Mode, (IY + asm_flag1)
 jr z, _
 RET

_

 ld e, BasicBuildings

Select_Buildings_Submenus:

 ld hl, Build_Basic_Buildings
 

 ld a,e
 cp BasicBuildings
 call z, Draw_Icon_Menu
 ld hl, Build_Research_Buildings
 ld a,e
 
 cp ResearchBuildings
 call z, Draw_Icon_Menu
 ld a,e
 cp BasicBuildings
 jr z, Select_Buildings_Submenus 
 ret


AddObjectToGame:  

 BIT Object_Ready_To_Add, (IY + asm_flag1)
 jr nz, _

 SET Object_Ready_To_Add, (IY + asm_flag1)
 CALL Rotate_Select_Mode
 ld a,c
 ld (BuildingSelected),a
 RET




_

 RES Object_Ready_To_Add, (IY + asm_flag1)
 CALL Rotate_Select_Mode
 ld a, (BuildingCount)

 ld h,0
 ld l,a
 
 ld d,h
 ld e,l
 call multiply_de_by_11

 ex de, hl
 ld hl, Objects_Address
 
 add hl, de
 push hl
 
 ld hl,(MapX)
 
 ld a, (Select_Cursor_X)
 ld d, 0
 ld e,a
 add hl, de
 ex de, hl
 pop hl

 ld (hl), e
 inc hl
 ld (hl),d
 inc hl

 push hl
 ld hl, (MapY)


 ld a, (Select_Cursor_Y)
 ld d, 0
 ld e,a
 add hl, de
 ex de, hl
 pop hl


 ld (hl), e

 inc hl
 ld (hl),d
 inc hl
 

 ld a, (BuildingSelected)

Continue:


 ld (hl), a
 push af

 push hl
 
	ld h,0
	add a, a
	ld l, a
	 

	ld de, Structure_Graphics_Addresses
	
	
	add hl, de
	
	ld e,(hl)
	inc hl
	ld d,(hl)

	push de
	pop ix
	
	pop hl
	inc hl
	

;Store the number of HP the building has, for debug only
	pop af
	push hl
	ld hl, Operation_Center
	or a
	ld de, 23
	sbc hl, de
	ld b,a
	inc b
_
	add hl, de
	djnz -_
	ex de, hl
	inc de
	inc de
	pop hl
	push ix
	push de
	
	pop ix
	ld b, (ix)
 	ld c,(ix+1)
	ld (hl), b
	inc hl
	ld (hl), c
	inc hl
	inc hl
	inc hl
	pop ix

	ld d,0
	ld e, (ix)
	ex de, hl
	add hl, hl
	add hl, hl
	add hl, hl
	ex de, hl

	ld (hl), e  ;Stores Building Width

	inc hl
	ld d, (ix+1)
	ld (hl), d ;Stores Builiding Height



 ld hl, BuildingCount
 inc (hl)
 ret





Draw_Structures_On_Map:
 
 



	ld a, (BuildingCount)  ; A is the number of buildings
	
	or a			; If there are no buildings, they
				; don't need to be drawn

	jp z, Copy_Screen_After_Buildings

	
 	ld h,0
	ld l,a
 	dec l
	
 	ld d,h
 	ld e,l
 	call multiply_de_by_11

 	ex de, hl

	ld ix, Objects_Address
 	add ix, de

Check_If_Structure_In_Range:


 	dec a
	ex af, af' ;Saving the number of buildings left
		   	;to avoid extensive use of pushing and popping.
			;Originally, register b was used, which was
			;unoptimized.

	


	;ix currently holds X position of the building
	
	
	ld l, (ix + Structure_X_Position)
	ld h, (ix + Structure_X_Position + 1)

	

	ld de, (MapX)

	or a ;Reset the carry flag

	sbc hl, de  	;HL now contains the position of the building relative
			;to the screen coordinates
	

	or a
    	ld    de, 96  
    	sbc    hl, de
    	add hl, de
    	jr    c, Check_Y_Coordinate_In_Range 

    	ld    e, (ix + Structure_Width)

    	add    hl, de
	dec hl
    	jr    nc, Check_Next_Building

	

Check_Y_Coordinate_In_Range:
	
	ld c, l
	
	;ix + 2 holds the Y position of the building
	
	ld l, (IX + Structure_Y_Position)
	ld h, (IX + Structure_Y_Position + 1)
	

	ld de, (MapY)
	
	or a ;Reset Carry Flag
	sbc hl, de

    	ld    de, 64
   	or    a     
   	sbc    hl, de
    	add    hl, de
    	jr    c, Success_So_Draw_Building


    	ld    e, (ix + Structure_Height)

  
    	add    hl, de
	dec hl
    	jr    nc, Check_Next_Building



Success_So_Draw_Building: ;The building is in range of the screen.
	
	ld b, l
	
	ld a, (IX + Structure_Type_ID) ;Register A is the location in the list
	   ;of addresses
 	


;Grab the right building address
	
	

	
	add a, a
	ld h,0
	ld l,a
	

	ld de, Structure_Graphics_Addresses
	add hl, de
	
	ld e, (hl)
	inc hl
	ld d, (hl)

	ex de, hl  ; HL points to the sprite data, starting
		   ; with the width

	ld d, c
	ld e, b
	
	ld c, (hl)
	inc hl
	ld b, (hl) 
	inc hl

	
	push ix
	push hl
	push hl
	pop ix

	di
	pop iy
	ld e, (ix)
	ld d, 0
	add iy, de

	
	call LargeClippedSprite
	
	ld iy, flags
	ei
	
	
	pop ix

Check_Next_Building:


	
	ld de, 11
	add ix, de

		
	ex af, af'
	
	or a
	jp nz, Check_If_Structure_In_Range


;==================================================================
;==================================================================
;==================================================================
;==================================================================
;==================================================================


Copy_Screen_After_Buildings:

	BIT Select_Mode, (IY + asm_flag1)
	ret nz
	ld hl, plotsscreen
	call safeCopy
	
	ret

multiply_de_by_11:

 add hl, hl
 add hl, hl
 add hl, de
 add hl, hl
 add hl, de


 ret

fastCopy:
	di
	ld a,$80
	out ($10),a
	ld hl,plotsscreen-12-(-(12*64)+1)
	ld a,$20
	ld c,a
	inc hl
	dec hl
_
	ld b,64
	inc c
	ld de,-(12*64)+1
	out ($10),a
	add hl,de
	ld de,10
_
	add hl,de
	inc hl
	inc hl
	inc de
	ld a,(hl)
	out ($11),a
	dec de
	djnz -_
	ld a,c
	cp $2B+1
	jr nz,--_
	ei
	ret	
 

